package com.pisolution.scheduler.runtime.queue;

import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * 带有优先级的任务队列.
 *
 * @author sora
 * @version 0.1.0
 */
@Service
public class TaskQueueService implements TaskQueue<String> {
    /** 用于存储待执行的任务对象. */
    private final PriorityBlockingQueue<String> queue;

    // -------------------------------------------------------------------------------------------------
    //  初始化
    // -------------------------------------------------------------------------------------------------

    public TaskQueueService() {
        this.queue = new PriorityBlockingQueue<>(3000, Comparator.naturalOrder());
    }

    // -------------------------------------------------------------------------------------------------
    //  任务访问
    // -------------------------------------------------------------------------------------------------

    @Override
    public void put(String task) {
        this.queue.put(task);
    }

    @Override
    public String take() throws Exception {
        return this.queue.take();
    }

    @Override
    public int size() {
        return this.queue.size();
    }
}
